package com.yx.cloud.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.yx.cloud.R;

/**
 * 引导界面 小点点 自动添加
 * mPointLayoutl.initViews(5, 30, 10);
 * mPointLayoutl.changePosition(i);//设置小点点选择
 */
public class PointLineLayout extends LinearLayout {

    private final static String TAG = "PointLineLayout";

    private int itemDefaultBgResId = R.mipmap.main_cursor_uncheck;//单个元素默认背景样式
    private int itemSelectedBgResId = R.mipmap.main_cursor_check;//单个元素选中背景样式
    private int itemSelectedBgResId2222 = R.mipmap.add_sweep_area;
    private int itemSelectedBgResId3333 = R.mipmap.add_default_icon;
    private int currentPosition;//当前选中位置
    private int itemHeight = 50;//item宽高
    private int itemMargin = 30;//item间距

    public PointLineLayout(Context context) {
        this(context, null, 0);
    }

    public PointLineLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PointLineLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
    }

    public void initViews(int count) {
        initViews(count, this.itemHeight, this.itemMargin);
    }

    public void initViews(int count, int itemHeight) {
        initViews(count, itemHeight, this.itemMargin);
    }

    /**
     * @param count：添加的个数
     * @param itemHeight：显示的大小
     * @param itemMargin       ：间距
     */
    public void initViews(int count, int itemHeight, int itemMargin) {
        currentPosition = 0;
        this.itemHeight = itemHeight;
        this.itemMargin = itemMargin;
        removeAllViews();
        if (count == 0 || itemHeight == 0) {
            return;
        }
        View view = createView(itemHeight, itemMargin);
//        view.setBackgroundResource(itemSelectedBgResId);
//        addView(view);
        if (count == 1) {
            return;
        }
        for (int i = 1; i <= count; i++) {
//            Log.i(TAG, i + "-------------: " + count);
            view = createView(itemHeight, itemMargin);
            if (i == count) {
                view.setBackgroundResource(itemDefaultBgResId);
            } else {
                view.setBackgroundResource(itemDefaultBgResId);
            }
            addView(view);
        }
    }

    /**
     * 创建view
     *
     * @param sideLength 边长
     * @param itemMargin 外间距
     * @return
     */
    public View createView(int sideLength, int itemMargin) {
        TextView textview = new TextView(getContext());
        LinearLayout.LayoutParams params = new LayoutParams(sideLength, sideLength);
        if (itemMargin > 0) {
            params.setMargins(itemMargin, 0, itemMargin, 0);
        }
        textview.setLayoutParams(params);
        return textview;
    }

    //切换到目标位置
    public void changePosition(int position) {
        if (getChildCount() <= 1) {
            return;
        }
        if (position == getChildCount() - 1) {
            getChildAt(currentPosition).setBackgroundResource(itemDefaultBgResId);
            currentPosition = position % getChildCount();
            getChildAt(currentPosition).setBackgroundResource(itemSelectedBgResId);
        } else {
            getChildAt(currentPosition).setBackgroundResource(itemDefaultBgResId);
            getChildAt(getChildCount() - 1).setBackgroundResource(itemDefaultBgResId);
            currentPosition = position % getChildCount();
            getChildAt(currentPosition).setBackgroundResource(itemSelectedBgResId);
        }
    }
}
